// 初始化PanelOpt对象
var userAuthorityMgrPanelOpt = {
    id:"userAuthorityMgrPanel",
    bombType:BOMB_TYPE2
};

// 用户机构、角色权限编辑提交
function toSubUserOrgRole() {

    var orgTreeObj = $.fn.zTree.getZTreeObj("orgSel_tree");
    var roleTreeObj = $.fn.zTree.getZTreeObj("roleSel_tree");
    var checkedOrgNodes = orgTreeObj.getCheckedNodes();
    var checkedRoleNodes = roleTreeObj.getCheckedNodes();
    var userId = $("#userAuthorityMgr_userIId").val();

    var orgIDs = "";
    var roleIDs = "";
    if(checkedOrgNodes){
        $(checkedOrgNodes).each(function(i,obj){
            if(i+1==checkedOrgNodes.length){
                orgIDs += obj.realID;
                return;
            }
            orgIDs += obj.realID+",";
        });
    }
    if(checkedRoleNodes ){
        $(checkedRoleNodes).each(function(i,obj){
            // 排除系统角色和项目角色顶级默认角色外
            if (obj.id == 1 || obj.id == 2) {
                // continue;
                return;
            } else {
                if(i+1==checkedRoleNodes.length){
                    roleIDs += obj.realID;
                    return;
                }
                roleIDs += obj.realID+",";
            }

        });
    }
    // 提交保存
    $.wdfProgress.progress("请稍等...",$("body"));
    $.ajax({
        url:basePath+"/user/addOrUpdateOrgRole",
        data:{
            id: userId,
            orgIDs:orgIDs,
            roleIDs:roleIDs,
            cType:"PCWEB"
        },
        dataType:"json",
        type: "post",
        contentType:"application/x-www-form-urlencoded",
        beforeSend:null,
        success:function(data){
            if(data.status==1){

                $.wdfProgress.showMsg("数据加载成功！",$("body"),true);
                $.wdfProgress.progRemove($("body"));
                userAuthorityMgrPanel.close();

                userGridOpt.keepPage=true;
                $("#userGrid").wdfGrid("reload",userGridOpt);
            }
        },
        error:function (XMLHttpRequest, textStatus, errorThrown) {
            $.wdfProgress.showMsg("系统错误或网络延迟！",$("body"),true);
        }
    });
}

// 机构下拉树初始化
var orgZTreeSetting = {
    data : {
        simpleData : {
            enable : true
        }
    },
    check: {
        enable: true,
        chkStyle:"checkbox",
        chkboxType: { "Y": "", "N": "" }
    },
    callback : {
        //onCheck:orgOnCheck, // 机构与角色无任何关联
        onClick: function (e, treeId, treeNode, clickFlag) {
            var orgTree =  $.fn.zTree.getZTreeObj("orgSel_tree");
            orgTree.checkNode(treeNode, !treeNode.checked, true);
        }
    }
};

// 角色下拉树初始化
var roleZTreeSetting = {
    data : {
        simpleData : {
            enable : true
        }
    },
    check: {
        enable: true,
        chkStyle:"checkbox",
        chkboxType: { "Y": "", "N": "" }
    },
    callback : {
        //onCheck:orgOnCheck, // 机构与角色无任何关联
        onClick: function (e, treeId, treeNode, clickFlag) {
            var orgTree =  $.fn.zTree.getZTreeObj("roleSel_tree");
            orgTree.checkNode(treeNode, !treeNode.checked, true);
        }
    }
};

// 机构全选事件
function checkAuthorityAllOrg(e) {
    var orgTree =  $.fn.zTree.getZTreeObj("orgSel_tree");
    if($(e).hasClass("uncheckBtn")){
        $(e).removeClass("uncheckBtn");
        $(e).addClass("checkedBtn");
        $(e).prev("input[type=checkbox]").prop("checked",true);
        orgTree.checkAllNodes(true);
    }else{
        $(e).removeClass("checkedBtn");
        $(e).addClass("uncheckBtn");
        $(e).prev("input[type=checkbox]").prop("checked",false);
        orgTree.checkAllNodes(false);
    }
}

// 角色全选事件
function checkAuthorityAllRole(e){
    var zTreeObj = $.fn.zTree.getZTreeObj("roleSel_tree");
    if($(e).hasClass("uncheckBtn")){
        $(e).removeClass("uncheckBtn");
        $(e).addClass("checkedBtn");
        $(e).prev("input[type=checkbox]").prop("checked",true);
        zTreeObj.checkAllNodes(true);
    }else{
        $(e).removeClass("checkedBtn");
        $(e).addClass("uncheckBtn");
        $(e).prev("input[type=checkbox]").prop("checked",false);
        zTreeObj.checkAllNodes(false);
    }
}

//用户机构、权限设置
function addUserOfAuthority(id, name) {
    $("#userAuthorityMgr_tit").find(".userAuthorityMgr_tab").eq(0).addClass("indexAuthTab");
    $("#checkAuthorityAllOrgBtn").attr("class","checkAllBtn uncheckBtn");
    $("#orgAllCheck").prop("checked",false);
    $("#user_systemAuthority_box").show();
    $("#userAuthorityMgr_userIId").val(id);

    var rowDatas=$("#userGrid").wdfGrid("getRowDatasByDK",{key:"id",value:id});
    var user=rowDatas[0];
    userAuthorityMgrPanel.find("#userId_addform").val(user.id);
    userAuthorityMgrPanel.find("#userName_addform").val(user.userName);
    userAuthorityMgrPanel.find("#password_addform").val("******");
    userAuthorityMgrPanel.find("#name_addform").val(user.name);
    userAuthorityMgrPanel.find("#sex_addform").val(user.sex);
    userAuthorityMgrPanel.find("#email_addform").val(user.email);
    userAuthorityMgrPanel.find("#phone_addform").val(user.phone);
    userAuthorityMgrPanel.find("#description_addform").val(!user.description ? "":user.description);
    userAuthorityMgrPanel.find("#department_addform").val(user.departmentName);
    userAuthorityMgrPanel.show("设置用户 ("+user.userName+") 机构角色信息");

    var departmentsArray = [];
    var rolesArray = [];

    $.wdfProgress.progress("请稍等...",$("body"));
    $.ajax({
        url:basePath+"/department/listTree",
        data:{

        },
        dataType:"json",
        type: "post",
        contentType:"application/x-www-form-urlencoded",
        beforeSend:null,
        success:function(data){
            data = data.data;
            $.fn.zTree.init($("#orgSel_tree"),orgZTreeSetting,data);
            var orgTree =  $.fn.zTree.getZTreeObj("orgSel_tree");
            orgTree.checkAllNodes(false);
            orgTree.expandAll(true);

            $.ajax({
                url:basePath+"/role/listTree",
                data:{

                },
                dataType:"json",
                type: "post",
                contentType:"application/x-www-form-urlencoded",
                beforeSend:null,
                success:function(data){
                    /**获取所有节点*/
                    data = data.data;
                    // 先设置节点复选框属性值,再进行下拉树初始化
                    $(data).each(function(i,obj){
                        //过滤数据，如果角色名称为：系统角色、项目角色,则不让其展示checkbox,不让其选择
                        if(obj.id == 1 || obj.id == 2){
                            //z-tree数据结构属性
                            obj["nocheck"] = true;
                        }
                    });

                    $.fn.zTree.init($("#roleSel_tree"),roleZTreeSetting,data);
                    var roleTree =  $.fn.zTree.getZTreeObj("roleSel_tree");
                    roleTree.checkAllNodes(false);
                    roleTree.expandAll(true);

                    // 用户机构与角色信息
                    if(user.departmentIds != null){
                        if(user.departmentIds.length > 0){
                            $(user.departmentIds.split(",")).each(function (i, obj) {
                                departmentsArray.push(obj);
                            });
                        }
                    }
                    if(user.roleIDS != null){
                        if(user.roleIDS.length > 0){
                            $(user.roleIDS.split(",")).each(function (i,obj) {
                                rolesArray.push(obj);
                            });
                        }
                    }
                    for (var i=0;i<departmentsArray.length;i++) {
                        var checkNode = orgTree.getNodeByParam("realID",departmentsArray[i], null);
                        orgTree.checkNode(checkNode,true,true);
                    }
                    for (var i=0;i<rolesArray.length;i++) {
                        var checkNode = roleTree.getNodeByParam("realID",rolesArray[i], null);
                        roleTree.checkNode(checkNode,true,true);
                    }

                    $.wdfProgress.showMsg("数据加载成功！",$("body"),true);
                    $.wdfProgress.progRemove($("body"));
                },
                error:function (XMLHttpRequest, textStatus, errorThrown) {

                }
            })

        },
        error:function (XMLHttpRequest, textStatus, errorThrown) {
            $.wdfProgress.showMsg("系统错误或网络延迟！",$("body"),true);
        }
    });

}

$(function () {
    // 初始化用户机构、权限编辑页面
    userAuthorityMgrPanel=$("#userAuthorityMgrPanel").wdfPanel("FormPanel",userAuthorityMgrPanelOpt);

})